//logs.js
const util = require('../../utils/util.js')
const { $Toast } = require('../../dist/base/index');
var api = require('../../config/api.js');
const app = getApp();

Page({
  data: {

    loading:false,

    roomIndex:0,
    roomIds:[],

    /*表单参数*/
    rooms: [],
    paterId: null,
    name: null,
    num: null,
    dmtIs: null,
    sjdA:1,
    sjdB:1,
    sjdC:1,
  },
  onLoad: function () {
  },

  onShow(){
    let self = this;
    util.request(api.BuildList, {
    }, 'POST').then(res => {
      console.log(res)
      let roomIds = []
      for (let i=0; i<res.length; i++){
        roomIds.push({'index':i, 'id':res[i].id})
      }
      self.setData({
        rooms: res,
        roomIds: roomIds
      })
    }).catch(err => {
      console.log(err)
    });
  },



  addRoom: function(){
    let slef = this;
    this.setData({
      loading: true
    });
    util.request(api.AddRoom, {
      paterId: slef.data.paterId,
      name: slef.data.name,
      num: slef.data.num,
      dmtIs: slef.data.dmtIs,
      sjdA: slef.data.sjdA,
      sjdB: slef.data.sjdB,
      sjdC: slef.data.sjdC,
    }, 'POST').then(res => {
      if (res.success) {
        $Toast({
          content: res.msg,
          type: 'success'
        });
        setTimeout(function () {
          wx.navigateBack();
        }, 1500)
      }
    }).catch(err => {

    });
  },


  clickFloor(e){
    let paterId;
    for (let i = 0; i < this.data.roomIds.length; i++){
      if (this.data.roomIds[i].index == e.detail.value){
        paterId = this.data.roomIds[i].id;
        break;
      }
    }
    this.setData({
      roomIndex: e.detail.value,
      paterId: paterId
    })
  },


  inputName(e){
    this.setData({
      name: e.detail.value
    })
  },

  inputNum(e){
    this.setData({
      num: e.detail.value
    })
  },
  clickDmtIs(e) {
    this.setData({
      dmtIs: e.detail.value ? 1 : 0
    })
  },
  checkboxChange(e) {
    this.setData({
      sjdA: 0,
      sjdB: 0,
      sjdC: 0,
    })
    for (let i = 0; i < e.detail.value.length; i++){
      let name = e.detail.value[i];
      this.setData({
        [name]: 1
      })
    }
    console.log(this.data.sjdA + "," + this.data.sjdB + "," + this.data.sjdC);
  },
})
